cordova.define("mamp-plugin-systemAbility", function(require, exports, module) {

var exec = require('cordova/exec');
var capture = require('cordova-plugin-media-capture.capture');
var camera = require('cordova-plugin-camera.camera');
var Camera = require('cordova-plugin-camera.Camera');

function systemAbility(){}
/*
 desc:调用系统打电话功能
 mobile:电话号码
 */
systemAbility.prototype.tel = function(mobile) {
    exec(null,null,"SystemAbility","tel",[mobile]);
};
               
/*
 desc:调用系统发短信功能
 mobile:电话号码
 */
systemAbility.prototype.sms = function(mobile) {
    exec(null,null,"SystemAbility","sms",[mobile]);
};
               
/*
 desc:调用并打开系统浏览器
 url:打开地址（绝对地址）
 */
systemAbility.prototype.openUrl = function(url) {
    exec(null,null,"SystemAbility","openUrl",[url]);
};

/*
 desc:创建事件时默认可选参数
 title:标题
 location:位置描述
 notes:描述
 startDate:开始时间
 endDate:结束时间
 firstReminderMinutes:第一次提醒提前时间，单位分钟
 secondReminderMinutes:第二次提醒提前时间，单位分钟
 recurrence:重复类型，按天，周，月，年
 recurrenceInterval:重复间隔，例如recurrence为周，interval设置为3的话表示每3周执行一次
 recurrenceEndDate:重复结束时间
 
 */
systemAbility.prototype.getCalendarOptions = function () {
    return {
        title:'',
        location:'',
        notes:'',
        startDate:null,
        endDate:null,
        firstReminderMinutes: 60,
        secondReminderMinutes: null,
        recurrence: null, // options are: 'daily', 'weekly', 'monthly', 'yearly'
        recurrenceInterval: 1, // only used when recurrence is set
        recurrenceEndDate: null
    };
};
/*
 desc:向系统日历添加日程
 title:标题
 location:位置描述
 notes:描述
 startDate:开始时间
 endDate:结束时间
 option:可选参数，参考getCalendarOptions
 successCallback:成功回调
 errorCallback:失败回调
 */
systemAbility.prototype.createCalendarEvent = function(config, successCallback, errorCallback) {
    var title = config.title;
    var location = config.location;
    var notes = config.notes;
    var startDate = config.startDate;
    var endDate = config.endDate;
               
   if (!(startDate instanceof Date && endDate instanceof Date)) {
       errorCallback("startDate and endDate must be JavaScript Date Objects");
       return;
   }
   // merge passed options with defaults
   var mergedOptions = systemAbility.prototype.getCalendarOptions();
   for (var val in config) {
       if (config.hasOwnProperty(val)) {
           mergedOptions[val] = config[val];
       }
   }
   if (config != null && config.recurrenceEndDate != null) {
        mergedOptions.recurrenceEndTime = config.recurrenceEndDate.getTime();
   }
   cordova.exec(successCallback, errorCallback, "SystemAbility", "createEventWithOptions", [{
   "title": title,
   "location": location,
   "notes": notes,
   "startTime": startDate instanceof Date ? startDate.getTime() : null,
   "endTime": endDate instanceof Date ? endDate.getTime() : null,
   "options": mergedOptions
   }]);
};
               
/*
 desc:调用系统相机进行拍照,把图片地址返回给JS
 callback:返回给JS时的回调方法
 front:可选参数，是否前置摄像头
 */
systemAbility.prototype.takeCamera = function(callback,isfront) {

var cameraDirection = Camera.Direction.BACK;
    if(isfront != null && isfront == true)
    {
        cameraDirection = Camera.Direction.FRONT;
    }
    camera.getPicture(function(url){
                 callback(url);
                 },function(msg){
                      
                 },{
                 quality : 50,
                 encodingType: Camera.EncodingType.JPEG,
                 destinationType:Camera.DestinationType.FILE_URI,
                 sourceType:Camera.PictureSourceType.CAMERA,
                 cameraDirection:cameraDirection
                 });
};
               
systemAbility.prototype.captureVideo = function(successCallback, errorCallback) {
    exec(successCallback, errorCallback, "Capture","captureVideo", []);
};
               
/*
 desc:调用系统相册选择图片
 callback:返回给JS时的回调方法
 limit:可选择张数，默认1张
 */
systemAbility.prototype.takePhoto = function(callback,limit) {
    camera.getPicture(function(url){
                 callback(url);
                 },function(msg){
                 
                 },{
                 quality : 50,
                 encodingType: Camera.EncodingType.JPEG,
                 destinationType:Camera.DestinationType.FILE_URI,
                 sourceType:Camera.PictureSourceType.PHOTOLIBRARY,
                 limit:limit
                 });
};

/*
 写入字符串到剪切板
 */
systemAbility.prototype.setPasteboard = function(string){
    exec(null, null, "SystemAbility","setPasteboard", [string]);
};
/*
 读取剪切板内容
 @param callback会返回剪切板的内容
 */
systemAbility.prototype.getPasteboard = function(callback){
    exec(callback, null, "SystemAbility","getPasteboard", []);
};

/*
  设置手机不锁屏
  @flag true表示永不锁屏，false会锁屏
*/
systemAbility.prototype.setIdleTimerDisabled = function(flag){
  exec(null, null, "SystemAbility","setIdleTimerDisabled", [flag]);
};

/*
  设置手机屏幕亮度
  @value 0到1,1是最亮
*/
systemAbility.prototype.setBrightness = function(value){
  exec(null, null, "SystemAbility","setBrightness", [value]);
};

               
module.exports = new systemAbility();
});
